package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.ywjh.inter.entity.BindInsulinEntity;

public class BindInsulinDAO {

	public boolean insert(Connection conn, BindInsulinEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"insert into bind_insulin(equ_name,equ_mac,patient_id,doc_id,add_time,status) values(?,?,?,?,?,?)");
		ps.setString(1, entity.getEqu_name());
		ps.setString(2, entity.getEqu_mac());
		ps.setInt(3, entity.getPatient_id());
		ps.setInt(4, entity.getDoc_id());
		ps.setTimestamp(5, entity.getAdd_time());
		ps.setInt(6, entity.getStatus());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public BindInsulinEntity findByEqu(Connection conn, String equ_name, String equ_mac) throws SQLException {
		BindInsulinEntity entity = null;
		PreparedStatement ps = conn
				.prepareStatement("select * from bind_insulin where equ_name=? and equ_mac=? and status=1");
		ps.setString(1, equ_name);
		ps.setString(2, equ_mac);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			entity = fill(rs);
		}
		return entity;
	}

	private BindInsulinEntity fill(ResultSet rs) throws SQLException {
		BindInsulinEntity e = new BindInsulinEntity();
		e.setId(rs.getInt("id"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setDoc_id(rs.getInt("doc_id"));
		e.setEqu_mac(rs.getString("equ_mac"));
		e.setEqu_name(rs.getString("equ_name"));
		e.setPatient_id(rs.getInt("patient_id"));
		e.setStatus(rs.getInt("status"));
		return e;
	}

	public BindInsulinEntity findBypatientId(Connection conn, int info_id) throws SQLException {
		BindInsulinEntity entity = null;
		PreparedStatement ps = conn.prepareStatement("select * from bind_insulin where patient_id=? and status=1");
		ps.setInt(1, info_id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			entity = fill(rs);
		}
		return entity;
	}

	public boolean update(Connection conn, BindInsulinEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update bind_insulin set equ_name=?,equ_mac=?,patient_id=?,doc_id=?,add_time=?,status=? where id=?");
		ps.setString(1, entity.getEqu_name());
		ps.setString(2, entity.getEqu_mac());
		ps.setInt(3, entity.getPatient_id());
		ps.setInt(4, entity.getDoc_id());
		ps.setTimestamp(5, entity.getAdd_time());
		ps.setInt(6, entity.getStatus());
		ps.setInt(7, entity.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public int findPatientByEqu(Connection conn, String equ_name, String equ_mac, Timestamp check_time) {
		int id = 0;
		try {
			PreparedStatement ps = conn.prepareStatement(
					"select patient_id from bind_insulin where equ_name=? and equ_mac=? and add_time<=? and status=1");
			ps.setString(1, equ_name);
			ps.setString(2, equ_mac);
			ps.setTimestamp(3, check_time);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return id;
	}

	public int findPatientByEqu(Connection conn, String equ_name, String equ_mac) {
		int id = 0;
		try {
			PreparedStatement ps = conn.prepareStatement(
					"select patient_id from bind_insulin where equ_name=? and equ_mac=? and status=1");
			ps.setString(1, equ_name);
			ps.setString(2, equ_mac);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return id;
	}

	public boolean findByMac(Connection conn, String mac) {
		boolean b=false;
		try {
			PreparedStatement ps = conn.prepareStatement(
					"select patient_id from bind_insulin where equ_mac=? and status=1");
			ps.setString(1, mac);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				b = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}

}
